package vnc.ui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Point;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;

import javax.swing.JPanel;

import vnc.VncFrame;
import vnc.ui.PacketBox;

import jpcap.packet.Packet;

public class MovingBox extends JPanel{
	Timer timer;
	JLabel jl;
	static int cnt=0;

	
	static PacketDetails pd;
	public static String iph;
	public static String mach;
	/*addrTable*/
	static int currentItem2;
	static int beforeCurrentItem2;
	static int wishItem2;
	
	static int currentItem3;
	static int beforeCurrentItem3;
	static int wishItem3;
	static Point startP3;
	static Point currentP3;
	static Dimension currentD3;
	static Dimension spaceD3;

	
	static Point startP2;
	static Point currentP2;
	
	static Dimension currentD2;
	static Dimension spaceD2;
	/*2*/
	
	
	
	public static BoxContainer bc;
	public static AddrTable at;
	//add
	public static boolean flag;
	
	static int currentItem;
	static int beforeCurrentItem;
	static int wishItem;
	
	static Point startP;
	static Point currentP;
	
	static Dimension currentD;
	static Dimension restD;
	static Dimension spaceD;

	public static List<PacketBox> boxes;
	//static List<AddrBox> tables;
	
	public static void removeBoxBorder(PacketBox box){
		box.setBorderPainted(false);
		box.setContentAreaFilled(false);
		box.setDefaultCapable(false);
		box.setFocusPainted(false);
	}
	
	public void clear(){
		bc.clear();
	}
	
	public void removeBox(int index){
		bc.removeBox(index);
	}

	
	public MovingBox(List<Packet> packets, int width, int height) {

		/*addrtable*/
		
		currentItem2 = 0;
		beforeCurrentItem2 = 0;
		wishItem2 = 0;
		
		currentItem3 = 0;
		beforeCurrentItem3 = 0;
		wishItem3 = 0;
		
		currentD2 = new Dimension(250, 80);
		spaceD2 = new Dimension(20,(369/2)-(currentD2.height/2));
	
		currentD3 = new Dimension(250, 80);
		spaceD3 = new Dimension(20,(369/2)-(currentD3.height/2));
		// location for boxes not for this panel
		startP2 = new Point(spaceD2.width , spaceD2.height);		//2,138
		currentP2 = new Point(spaceD2.width, spaceD2.height);

		startP3 = new Point(spaceD3.width , spaceD3.height);		//2,138
		currentP3 = new Point(spaceD3.width, spaceD3.height);
		
		/*/////////////////////*/
		//tables = new ArrayList<AddrBox>();
		
		
		currentItem = 0;
		beforeCurrentItem = 0;
		wishItem = 0;
		
		currentD = new Dimension(114, 114);
		restD = new Dimension(85, 85);
		spaceD = new Dimension((width/2)-(currentD.width/2), 2);
		
		boxes = new ArrayList<PacketBox>();
		
		// location for boxes not for this panel
		startP = new Point(spaceD.width, spaceD.height + (currentD.height - restD.height));
		currentP = new Point(width/2 - currentD.width/2, spaceD.height);
		
		this.setBackground(Color.white);
		
		this.setLayout(null);
		
		//add
		InetAddress in = null;
		NetworkInterface m;
		byte[] mm = null;
		try {
			in= InetAddress.getLocalHost();
			m = NetworkInterface.getByInetAddress(InetAddress.getLocalHost());
			mm = m.getHardwareAddress();
		} catch (SocketException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		this.iph = in.getHostAddress();
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < mm.length; i++) {
			sb.append(String.format("%02x%s", mm[i], (i < mm.length - 1) ? ":" : ""));		
		}
		mach = sb.toString();
		
		
		flag = true;
		at = new AddrTable(packets,mach,iph);
		
		this.add(at);

		
		at.setBounds(-1,0,1024,370);
		//at.setBorder(BorderFactory.createLineBorder(Color.white));
		
		bc = new BoxContainer(packets ,boxes,this);
		
		this.add(bc);
		
		pd = new PacketDetails();
		
		pd.setBounds(-1, 0, 1024, 370);
		
		
		// because of the border eating 1 pixel,, that's why put -1
		bc.setBounds(-1, 370, 1024, 150);
		
		bc.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
		
		bc.t.start();
		
		timer = new Timer();
		
		  
	}

	public class WorkTask extends TimerTask{
		
		  @Override
		  public void run() {
			  cnt++;
			  if(cnt%3==0){
				  jl.setText("Loading...");
				  cnt=0;
			  }
			  else if(cnt%2==0)
			  {
				  jl.setText("Loading..");
			  }
			  else{
				  jl.setText("Loading.");
			  }
			  
				if(!bc.lastPoint){
					at.macPane.remove(jl);
					//jl.setVisible(false);
					bc.saveBox();
					at.saveTable();
					
					
					
					timer.cancel();		
				}
		  }		  
	}
	public void tableStart(){
		bc.stopRun();
		WorkTask wt = new WorkTask();
		jl = new JLabel();
		jl.setFont(new Font("",Font.BOLD,25));
		
		jl.setText("Loading");
		jl.setBounds(110, 50, 200, 100);
		at.macPane.add(jl);
		jl.setVisible(true);
		
		timer.schedule(wt, 5000, 3000);	

			
	}

}

